package cn.e2win.biz.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.e2win.base.entity.*;
import cn.e2win.biz.user.UserDao;
import cn.e2win.biz.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.cygs.CygsDao;
import cn.e2win.biz.cyzj.CyzjDao;
import cn.e2win.biz.dbo.BaseEmp;
import cn.e2win.biz.dbo.CygsEmp;
import cn.e2win.biz.dbo.CyzjEmp;
import cn.e2win.biz.dbo.DsEmp;
import cn.e2win.biz.dbo.DsRelation;
import cn.e2win.biz.dbo.GuestRelation;
import cn.e2win.biz.dbo.ZdzxEmp;
import cn.e2win.biz.ds.DsDao;
import cn.e2win.biz.util.StringUtils;


@Service
public class CommonService extends BaseService{
	

	@Autowired
	private CommonDao commonDao;
	@Autowired
	private DsDao dsDao;
	@Autowired
	private CyzjDao cyzjDao;
	@Autowired
	private CygsDao cygsDao;
	private static final Integer ROLE_ID_DS_EMP=-1;
	private static final Integer ROLE_ID_CYZJ_EMP=-2;
	private static final Integer ROLE_ID_CYZJ_LEADER=-3;
	private static final Integer ROLE_ID_CYGS_EMP=-4;
	private static final Integer ROLE_ID_CYGS_LEADER=-5;
	private static final Integer ROLE_ID_Zdzx_EMP=1;
	@Autowired
	private UserDao userDao;

	@Override
	public BaseDAO initDao() {
		return commonDao;
	}

	public void pagerApproveLog(Pager pager){
		commonDao.pagerApproveLog(pager);
	}
	


	public List<SysLabel> getSysLabelList(String className, String labelType){
		return commonDao.getSysLabelList(className, labelType);
	}


	public List<SysLabel> getSysLabelList(BaseEntity entity, String labelType){
		return commonDao.getSysLabelList(entity, labelType);
	}

	public SysLabelSet getSysLabelSet(SysLabel sysLabel, String label){
		return commonDao.getSysLabelSet(sysLabel, label);
	}
	


	public List<SysLabel> editLabel(String labels, SysLabel labelTpl) {
		// TODO Auto-generated method stub
		List<SysLabel> sysLabelList = new ArrayList<SysLabel>();
		try {
			if(!StringUtils.isEmpty(labels)) {
                String[] labelMain = labels.split(",");
                for (String label : labelMain) {
                    if("".equals(label.trim())){
                        continue;
                    }
                    SysLabel syslabel = new SysLabel();
                    syslabel.setEntityId(labelTpl.getEntityId());
                    syslabel.setLabelType(labelTpl.getLabelType());
                    syslabel.setEntityType(labelTpl.getEntityType());
                    syslabel.setLabel(label.trim());

                    syslabel = (SysLabel) edit(syslabel);
					sysLabelList.add(syslabel);
                }
            }
		} catch (MyException e) {
			e.printStackTrace();
		}

		return sysLabelList;
	}


	public void delLabel(List<SysLabel> sysLabelList){
		for (SysLabel sysLabel : sysLabelList) {
			commonDao.delete(sysLabel);
		}
	}


	/**
	 * example 1,2,3,
	 * @param delPicIds
     */
	public void delPic(String delPicIds){
		if(!StringUtils.isEmpty(delPicIds)) {
			for (String s : delPicIds.split(",")) {
				if (!StringUtils.isEmpty(s)) {
					Integer id = Integer.parseInt(s);
					SysAttach sysAttch = (SysAttach) commonDao.getEntity(SysAttach.class, id);
					if (sysAttch != null) {
						commonDao.delete(sysAttch);
					}
				}
			}
		}
	}

	public List<SysAttach> getSysAttachList(BaseEntity entity, String attachType){
		List<SysAttach> rList = null;
		if(entity != null && entity.getId()!=null){
			rList =  commonDao.getSysAttachList(entity,attachType);
		}
		return rList;
	}

	
	
	

	//======================================================================================================================================================================
	//与项目相关的方法
	//======================================================================================================================================================================
	

	public void pagerDsRelation(BaseEntity relatedObj, Pager pager) {
		commonDao.pagerDsRelation(relatedObj, pager);
	}
	
	public void saveDsRelationList(BaseEntity relatedObj,List<DsRelation> dsList){
		if(dsList!=null && dsList.size()>0){
			commonDao.delDsRelation(relatedObj);
			//保存导师列表
			for(DsRelation dsRelation : dsList){
				dsRelation.setBaseInfo(relatedObj);
			}
			super.saveAll(dsList);
		}
		
	}
	

	public void pagerGuestRelation(BaseEntity relatedObj, Pager pager) {
		commonDao.pagerGuestRelation(relatedObj, pager);
	}
	public void saveGuestRelationList(BaseEntity relatedObj,List<GuestRelation> guestList){
		if(guestList!=null && guestList.size()>0){
			commonDao.delGuestRelation(relatedObj);
			//保存导师列表
			for(GuestRelation dsRelation : guestList){
				dsRelation.setBaseInfo(relatedObj);
			}
			super.saveAll(guestList);
		}
		
	}
	
	//根据手机号获取工作人员信息
	public List<SysUser> getEmpByMobile(String mobile)throws  MyException{ 
		List<SysUser> list = userDao.getUserByAccount(mobile);
		return list;
	}
	
	//保存为用户
	public SysUser saveUser4Emp(BaseEmp emp) throws MyException{
		
		//TODO 有效性验证 ,getByEmpId, getByMobile
		//添加用户
		SysUser user = new SysUser(emp);
		user = (SysUser)super.edit(user);
		
		//设置角色
		SysUserRole userRole = new SysUserRole();
		userRole.setUserId(user.getId());
		if(emp instanceof CyzjEmp ){
			if("1".equals(emp.getIsLeader())){
				userRole.setRoleId(ROLE_ID_CYZJ_LEADER);
			}else{
				userRole.setRoleId(ROLE_ID_CYZJ_EMP);
			}
		}else if(emp instanceof DsEmp){
			userRole.setRoleId(ROLE_ID_DS_EMP);
		}else if(emp instanceof CygsEmp){
			if("1".equals(emp.getIsLeader())){
				userRole.setRoleId(ROLE_ID_CYGS_LEADER);
			}else{
				userRole.setRoleId(ROLE_ID_CYGS_EMP);
			}
		}else if(emp instanceof ZdzxEmp){
				userRole.setRoleId(ROLE_ID_Zdzx_EMP);
		}
		userRole = (SysUserRole)super.edit(userRole);
		
		return user;
	}
	
	


	public List<Map<String, Object>> getEmpList(String type, Map<String, String> paramMap) {
		Pager pager = new Pager();
		pager.setScMap(paramMap);
		pager.setReturnAll(true);
		
		if("Zdzx".equals(type)){

		}else if("Ds".equals(type)){
			dsDao.pagerDs(pager);
		}else if("Cyzj".equals(type)){
			cyzjDao.pagerCyzjEmp(pager);
		}else if("Cygs".equals(type)){
			cygsDao.pagerCygsEmp(pager);
			
		}
		return pager.getResultList();// dsDao.getEmpList(type, paramMap);
	}

	public void empLookupPager(String type,Pager pager) {
		if("Zdzx".equals(type)){
			
		}else if("Ds".equals(type)){
			dsDao.pagerDs(pager);
		}else if("Cyzj".equals(type)){
			cyzjDao.pagerCyzjEmp(pager);
		}else if("Cygs".equals(type)){
			cygsDao.pagerCygsEmp(pager);
		}
		//		dsDao.pagerEmp(type,pager);
	}

	public List<Map<String, Object>> getOrgList(String type, Map<String, String> paramMap) {
		Pager pager = new Pager();
		pager.setScMap(paramMap);
		pager.setReturnAll(true);
		if("Zdzx".equals(type)){

		}else if("Ds".equals(type)){

		}else if("Cyzj".equals(type)){
			cyzjDao.pagerCyzj(pager);
		}else if("Cygs".equals(type)){
			cygsDao.pagerCygs(pager);
		}
		return pager.getResultList();//return dsDao.getOrgList(type, paramMap);
	}

	public void orgLookupPager(String type,Pager pager) {
		if("Zdzx".equals(type)){

		}else if("Ds".equals(type)){

		}else if("Cyzj".equals(type)){
			cyzjDao.pagerCyzj(pager);
		}else if("Cygs".equals(type)){
			cygsDao.pagerCygs(pager);
		}
		//dsDao.pagerOrg(type,pager);
	}

	public void pagerLabel(Pager pager) {
		commonDao.pagerLabel(pager);
	}
	@Transactional
	public int managerLabel(SysLabel oldSysLabel, SysLabelSet sysLabelSet, String newLabel) throws MyException {
		super.edit(sysLabelSet);
		return commonDao.updateLabels(oldSysLabel, newLabel);
	}
	@Transactional
	public int removeLabels(SysLabel label, SysLabelSet sysLabelSet){
		
		commonDao.delLabelSets(sysLabelSet);
		return commonDao.delLabels(label);
	}

}
